package com.zw.mes.module.prodtaskmanager.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.module.prodtaskmanager.entity.VwPreEnquiryFile;
import com.zw.mes.module.prodtaskmanager.model.PreEnquiry;
import com.zw.mes.module.prodtaskmanager.model.ProChange;
import com.zw.mes.module.prodtaskmanager.model.ProjectModel;
import com.zw.mes.module.prodtaskmanager.model.ProjectSchedulingModel;
import com.zw.mes.module.prodtaskmanager.service.ProjectChangeQueryService;
import com.zw.mes.module.system.service.LogService;
import com.zw.mes.util.FTPUtil;
import com.zw.mes.util.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * 项目变更记录查询
 * @author XYH
 */
@Controller
public class ProjectChangeQueryController extends BaseController {

    @Autowired
    private ProjectChangeQueryService projectChangeQueryService;

    @Autowired
    private LogService logService;

    @GetMapping("/overview/projectChangeQuery")
    @RequiresPermissions("overview:projectChangeQuery:view")
    public String page(){
        return "/overview/projectChangeQuery/page";
    }
    /**
     * 查询项目变更
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/projectScheduling/selectProjectChangeList")
    @RequiresPermissions("overview:projectChangeQuery:view")
    public JsonResult selectProjectChangeList(ProjectSchedulingModel model) {
        startPage();
        return jsonResult(projectChangeQueryService.selectProjectChangeList(model));
    }

    /**
     * 变更通知查看页面
     * @param proChangeId
     * @param model
     * @return
     */
    @GetMapping("/projectChange/chgInfo/viewInfo/{proChangeId}")
    @RequiresPermissions("overview:projectChangeQuery:view")
    public String viewInfoPage(@PathVariable("proChangeId") Integer proChangeId, Model model) {
        ProjectModel projectChgInfo = projectChangeQueryService.selectChangeInfoById(proChangeId);
        model.addAttribute("projectChgInfo", projectChgInfo);
        model.addAttribute("proChangeId", proChangeId);
        ProChange proChange = projectChangeQueryService.selectByPrimaryKey(proChangeId);
        if (proChange.getPreEnqId() != null){
            PreEnquiry preEnquiry = projectChangeQueryService.selectPreEnqById(proChange.getPreEnqId());
            model.addAttribute("preFiles", projectChangeQueryService.findChangeFileListByEnquiryId(preEnquiry.getId(), preEnquiry.getOrigId()));
        }
        return "overview/projectChangeQuery/info/page";
    }

    /**
     * @methodName: downloadFile
     * @description: 变更查询下载
     * @param: id
     * @param: httpServletResponse
     * @return： void
     * @exception：
     * @author： 徐一贺 tel:18341916590
     * @designer： 陈欣，盖世奇
     * @leader： 陈欣 tel:18104194240
     * @date： 2021/4/7 9:58
     * @office: 智能控制设计科
     * @research institute： 智能控制设计所
     * @department： 机械设计院
     * @company: 辽宁忠旺机械设备制造有限公司
     * @group： 忠旺集团
     * @version: 1.0
     */
    @GetMapping("proChangeQuery/downloadFile/{id}")
    @RequiresPermissions("overview:projectChangeQuery:download")
    public void downloadFile(@PathVariable("id") Integer id, HttpServletResponse httpServletResponse){
        VwPreEnquiryFile vwPreEnquiryFile = projectChangeQueryService.getVwPreEnquiryFileByPrimary(id);
        String filename = vwPreEnquiryFile.getUrl();
        filename = "e:\\sale\\upload\\"+filename.substring(7).replace("/","\\");
        FileUtils.downloadFile(filename, httpServletResponse);
        logService.saveLog(
                "项目变更查询-->变更通知单-->下载附件",
                "");
    }
}
